package com.yw.fileupload.upload.service.impl;

import cn.hutool.core.util.IdUtil;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.yw.fileupload.upload.service.FileUploadService;
import com.yw.fileupload.upload.util.file.ConstantPropertiesUtils;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;

/**
 * @author hbw
 * @create 2021-04-26 20:51
 */
@Service
public class FileUploadServiceImpl implements FileUploadService {
    @Override
    public String nativeFile(MultipartFile file) {

        /**
         * 定义输入输出流
         */
        InputStream inputStream = null;
        FileOutputStream outputStream= null;

        try {

            /**
             * 从配置文件中获取存储路径
             */
            String path = ConstantPropertiesUtils.LOCAL_PATH;
            inputStream = file.getInputStream();
            /**
             * 获取文件名
             */
            String filename = file.getOriginalFilename();

            /**
             * 创建目标文件，传入路径
             */
            File targetFile = new File(path+filename);

            /**
             * 如果目标路径不存在，创建一个目标路径
             */
            if(!targetFile.getParentFile().exists()){
                targetFile.getParentFile().mkdir();
            }
            /**
             * 将目标文件传入输出流
             */
            outputStream = new FileOutputStream(targetFile);

            /**
             * 转换输入流、输出流
             */
            FileCopyUtils.copy(inputStream, outputStream);

            System.out.println(filename);
            return filename;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }finally {

            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }



    }

    @Override
    public String aliyun(MultipartFile file) {
        // Endpoint以杭州为例，其它Region请按实际情况填写。
        String endpoint = ConstantPropertiesUtils.EDN_POINT;
        // 阿里云主账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建RAM账号。
        String accessKeyId = ConstantPropertiesUtils.KEY_ID;
        String accessKeySecret = ConstantPropertiesUtils.KEY_SECRET;
        String bucketName = ConstantPropertiesUtils.BUCKET_NAME;

        try{

            // 创建OSSClient实例。
            OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

            InputStream inputStream = file.getInputStream();
            String datePath = new DateTime().toString("yyyy-MM-dd");
            String filename = file.getOriginalFilename();
            filename = IdUtil.fastSimpleUUID()+"_"+filename ;
            filename="yw-fileupload"+"/"+datePath+"/"+filename;


            // 创建PutObjectRequest对象。
            ossClient.putObject(bucketName,filename,inputStream);

            // 关闭OSSClient。
            ossClient.shutdown();
            String url = "https://"+ bucketName+"."+endpoint+"/"+filename;
            return url;

        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
}
